package org.light.framework.ioc;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.light.framework.util.ClassUtil;
import org.light.framework.util.ReflectionUtil;

public final class BeanHelper {

	private static final Map<Class<?>,Object> BEAN_MAP=new HashMap<Class<?>,Object>();
	
	static{
		Set<Class<?>> beanClassSet=ClassUtil.getBeanClassSet();
		for (Class<?> beanClass : beanClassSet) {
			Object obj=ReflectionUtil.newInstance(beanClass);
			BEAN_MAP.put(beanClass, obj);
		}
	}
	
	public static Map<Class<?>,Object> getBeanMap() {
		return BEAN_MAP;
	}
	
	@SuppressWarnings("unchecked")
	public static <T> T  getBean(Class<T> cls) {
		if(!BEAN_MAP.containsKey(cls)){
			throw new RuntimeException("无法加载类"+cls);
		}
		return (T)BEAN_MAP.get(cls);
	}
	
	public static void setBean(Class<?> cls,Object obj) {
		BEAN_MAP.put(cls, obj);
	}
}
